<template>
  <div class="app-container">
    <!-- 搜索和 操作的行为区 -->
    <div class="conduct">
      <el-input v-model="listQuery.keyword" placeholder="请输入内容" class="conduct-input">
        <el-button slot="append" icon="el-icon-search" @click="handleFilter" />
      </el-input>
      <el-button class="danger-btn">创建</el-button>
      <!-- <el-button class="danger-btn" @click="handleUpdate(-1, 'create')">创建</el-button> -->
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column type="expand" class-name="expand-padd1">
        <template slot-scope="props">
          <!-- 展开列的内容 二级 -->
          <el-table
            :key="tableKey2"
            v-loading="listLoading"
            :data="props.row.childrenList"
            border
            fit
            highlight-current-row
            style="width: 100%; border: 1px solid #0F8CFD;;"
            :show-header="false"
          >
            <el-table-column type="expand" class-name="expand-padd2">
              <template slot-scope="prop">
                <!-- 展开列的内容 三级 -->
                <el-table
                  :key="tableKey3"
                  v-loading="listLoading"
                  :data="prop.row.childrenList"
                  border
                  fit
                  highlight-current-row
                  style="width: 100%; border: 1px solid #ff5357;"
                  :show-header="false"
                >
                  <el-table-column type="expand" class-name="expand-padd3">
                    <div style="text-align: center; line-height: 40px;">暂无内容</div>
                  </el-table-column>
                  <el-table-column label="名称" align="center">
                    <template slot-scope="{row}">
                      <span>{{ row.name }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="编号" align="center">
                    <template slot-scope="{row}">
                      <span>{{ row.menuId }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="上级模块" align="center">
                    <template slot-scope="{row}">
                      <!-- <span>{{ row.parentId==0? '无' : list[parseInt(row.parentId)-1].name }}</span> -->
                      <span>上级{{ row.parentId }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="图标" align="center">
                    <template slot-scope="{row}">
                      <span v-if="row.icon==''">无</span>
                      <img v-else :src="row.icon">
                    </template>
                  </el-table-column>
                  <el-table-column label="类型" align="center">
                    <template slot-scope="{row}">
                      <span>{{ row.type==1? '菜单': '按钮' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="URL" align="center">
                    <template slot-scope="{row}">
                      <span>{{ row.url==''? '无':row.url }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="授权标识" align="center">
                    <template slot-scope="{row}">
                      <span>{{ row.perms }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template>
                      <span class="action action-update" @click="handleUpdate(row, 'updata')">修改</span>
                      <span class="action action-update">修改</span>
                      |
                      <span class="action action-delete" @click="handleDelete(row,$index)">删除</span>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column label="名称" align="center">
              <template slot-scope="{row}">
                <span>{{ row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="编号" align="center">
              <template slot-scope="{row}">
                <span>{{ row.menuId }}</span>
              </template>
            </el-table-column>
            <el-table-column label="上级模块" align="center">
              <template slot-scope="{row}">
                <!-- <span>{{ row.parentId==0? '无' : list[parseInt(row.parentId)-1].name }}</span> -->
                <span>上级{{ row.parentId }}</span>
              </template>
            </el-table-column>
            <el-table-column label="图标" align="center">
              <template slot-scope="{row}">
                <span v-if="row.icon==''">无</span>
                <img v-else :src="row.icon">
              </template>
            </el-table-column>
            <el-table-column label="类型" align="center">
              <template slot-scope="{row}">
                <span>{{ row.type==1? '菜单': '按钮' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="URL" align="center">
              <template slot-scope="{row}">
                <span>{{ row.url==''? '无':row.url }}</span>
              </template>
            </el-table-column>
            <el-table-column label="授权标识" align="center">
              <template slot-scope="{row}">
                <span>{{ row.perms }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template>
                <span class="action action-update" @click="handleUpdate(row, 'updata')">修改</span>
                |
                <span class="action action-delete" @click="handleDelete(row,$index)">删除</span>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.menuId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上级模块" align="center">
        <template slot-scope="{row}">
          <!-- <span>{{ row.parentId==0? '无' : list[parseInt(row.parentId)-1].name }}</span> -->
          <span>上级{{ row.parentId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template slot-scope="{row}">
          <span v-if="row.icon==''">无</span>
          <img v-else :src="row.icon">
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="{row}">
          <span>{{ row.type==1? '菜单': '按钮' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="URL" align="center">
        <template slot-scope="{row}">
          <span>{{ row.url==''? '无':row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column label="授权标识" align="center">
        <template slot-scope="{row}">
          <span>{{ row.perms }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template>
          <span class="action action-update" @click="handleUpdate(row, 'updata')">修改</span>
          |
          <span class="action action-delete" @click="handleDelete(row,$index)">删除</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改和添加的弹窗 -->
    <el-dialog title="模块设置" :visible.sync="dialogFormVisible" width="40%" center :close-on-click-modal="false">
      <div class="toggle-item">
        <template>
          <span class="radio-title">权限配置: </span>
          <el-radio-group v-model="toggle_radio">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
            <el-radio :label="3">按钮</el-radio>
          </el-radio-group>
        </template>
      </div>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <div v-show="toggle_radio==1">
          <el-form-item label="目录名称">
            <el-input v-model="temp.id" />
          </el-form-item>
          <el-form-item label="排序编号:">
            <el-input v-model="temp.userName" type="text" maxlength="18" placeholder="请输入账户名" />
          </el-form-item>
          <el-form-item label="目录图标">
            <el-input v-model="temp.password" name="password" />
          </el-form-item>
        </div>

        <div v-show="toggle_radio==2">
          <el-form-item label="菜单名称">
            <el-input v-model="temp.id" />
          </el-form-item>
          <el-form-item label="上级目录:">
            <el-input v-model="temp.userName" type="text" maxlength="18" placeholder="请输入账户名" />
          </el-form-item>
          <el-form-item label="排序编号">
            <el-select
              v-model="temp.roleId"
              filterable
              reserve-keyword
              placeholder="请选择编号"
              style="width: 100%"
              clearable
            >
              <!-- <el-option v-for="item in list_role" :key="item.roleId" :label="item.roleName" :value="item.roleId" /> -->
            </el-select>
          </el-form-item>
          <el-form-item label="URL">
            <el-select
              v-model="temp.roleId"
              filterable
              reserve-keyword
              placeholder="请选择URL"
              style="width: 100%"
              clearable
            >
              <!-- <el-option v-for="item in list_role" :key="item.roleId" :label="item.roleName" :value="item.roleId" /> -->
            </el-select>
          </el-form-item>
          <el-form-item label="授权标识">
            <el-input v-model="temp.password" name="password" />
          </el-form-item>
          <el-form-item label="菜单图标">
            <el-input v-model="temp.password" name="password" />
          </el-form-item>

        </div>

        <div v-show="toggle_radio==3">
          <el-form-item label="按钮名称">
            <el-input v-model="temp.id" />
          </el-form-item>
          <el-form-item label="上级菜单:">
            <el-input v-model="temp.userName" type="text" maxlength="18" placeholder="请输入账户名" />
          </el-form-item>
          <el-form-item label="授权标识">
            <el-input v-model="temp.password" name="password" />
          </el-form-item>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="danger-btn" @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button class="primary-btn" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <!-- 删除框的确认窗口 -->
    <el-dialog :visible.sync="dialogPvVisible" title="提示" width="30%" center :close-on-click-modal="false">
      <div class="del-data del-img"><img :src="require('../../assets/img/del.png')" alt=""></div>
      <div class="del-data del-notify">确定对账号进行【删除】？</div>
      <span slot="footer" class="dialog-footer">
        <el-button class="danger-btn" @click="dialogPvVisible = false">取消</el-button>
        <el-button class="primary-btn" @click="delData">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils' // 解析时间
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { delModule, addModule, editModule, listModule } from '@/api/system'
import { validPass } from '@/utils/validate'
const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'Module',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!validPass(value)) {
        callback(new Error('8~16位,包含大小写字母和数字, 可以有特殊字符'))
      } else { callback() }
    }

    return {
      tableKey: 0,
      tableKey2: 2,
      tableKey3: 3,
      list: null,
      listLoading: true,
      listQuery: {
        keyword: '' // 搜索关键字
      },
      calendarTypeOptions,
      temp: {
        id: undefined,
        userName: '',
        password: '',
        status: '',
        role: ''
      },
      dialogFormVisible: true,
      dialogStatus: '',
      del_id: '',
      del_index: null,
      dialogPvVisible: false,
      rules: {
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      toggle_radio: 1
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取 table 列表
    getList() {
      this.listLoading = true
      listModule(this.listQuery.keyword).then(res => {
        this.list = res.data

        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      })
    },

    // 点击了 '编辑' 或添加======================================================
    handleUpdate(row, str) {
      if (str == 'create') this.resetTemp() // 清空
      if (str == 'updata') this.temp = Object.assign({}, row) // 克隆一个对象
      this.dialogStatus = str
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 编辑: 确认更新数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const cryptograph = this.$sha256(this.temp.password + this.temp.userName)
          editModule(this.temp.id, cryptograph, this.temp.roleId, this.temp.status, this.temp.userName).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id) // 找到被更改的item下标
            this.list.splice(index, 1, this.temp) // 通过下标更改list的数据
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 4000
            })
          })
        }
      })
    },
    // 确认添加了一个管理员
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const cryptograph = this.$sha256(this.temp.password + this.temp.userName)
          // let data2 = Object.assign({}, this.temp)   // 克隆一个对象

          addModule(cryptograph, this.temp.roleId, this.temp.status, this.temp.userName).then(() => {
            // this.list.unshift(this.temp)
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 4000
            })
          })
        }
      })
    },
    // 清空temp
    resetTemp() {
      this.temp = {
        id: undefined,
        userName: '',
        password: '',
        status: '',
        role: ''
      }
    },
    // 点击了删除===============================================
    handleDelete(row, index) {
      this.del_id = row.id
      this.del_index = index
      this.dialogPvVisible = true
    },

    // 确认了删除
    delData() {
      const ids = [this.del_id]
      delModule(ids).then(() => {
        this.dialogPvVisible = false
        this.list.splice(this.del_index, 1)
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 4000
        })
      })
    },

    // 搜索
    handleFilter() {
      this.getList()
    }
  }
}
</script>

<style lang="scss">
  // .expand-padd {
  //   padding:0 !important;
  //   background-color: antiquewhite;
  // }
  .el-table__expanded-cell {
    padding: 0 !important;
    background-color: antiquewhite;
  }

</style>
<style lang="scss" scoped>
  $theme_text: #0F8BFD;
  $backg:rgba(0,0,0,0);
  $deep_text: #5E5E5E;
  $theme_red: #ff5357;

  .app-container {
    // background-color: #f99 !important;
    .conduct {
      position: absolute;
      right: 0;
      top: 0;
      z-index: 9;
      width:50%;
      display: flex;
      justify-content: space-between;
      .conduct-input {
        margin-right: 10px;
        border: 0;
        input {
          background: transparent;
          border: 0;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          color: $deep_text;
          // height: 47px;
          caret-color: $deep_text;

          &:-webkit-autofill {
            box-shadow: 0 0 0 1000px $backg inset !important;
            -webkit-text-fill-color: $deep_text !important;
          }
        }
        .el-button {
          color: #fff;
          background-color: #0F8CFD;
        }
      }
    }

    // background-color: #f99 !important;
    .el-table {
      .action {
        cursor: pointer;
        &:hover {
          text-decoration: underline;
        }
      }
      .action-update {
        color: $theme_text;
      }
      .action-delete {
        color: $theme_red;
      }
    }

    .el-dialog {
      .del-data {
        text-align: center;
        margin-bottom: 16px;
        img {
          width: 110px;
          height: 110px;
        }
      }
      .del-img {}

      .toggle-item {
        margin: 0 0 30px 50px;
        // background-color: #cdf;
        .radio-title {
          font-size: 15px;
          font-weight: 600;
          margin-right: 18px;
        }
      }
    }
  }
</style>
